﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Services;

namespace HoneyBadgersOfficePlayer.WebService
{
    /// <summary>
    /// Summary description for SongStatus
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class SongStatus : System.Web.Services.WebService
    {
        [WebMethod(true)]
        public string getNewSong(int roomID)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@room_id", roomID));

            SqlDataReader reader;
            reader = SQLUtilities.RunStoredProcedure("DemoStreaming", parameterList);

            if (reader != null && reader.HasRows)
            {
                reader.Read();
                {
                    return "" + reader["File_location"] + "|" + reader["Artist"] + "|" + reader["Song Name"] + "|" + reader["Duration"] + "|" + reader["Song_ID"];
                }
            }
            else
            {
                return "";
            }
        }

        [WebMethod(true)]
        public string getCurrentSong(int roomID)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@room_id", roomID));

            SqlDataReader reader;
            reader = SQLUtilities.RunStoredProcedure("GetCurrentSong", parameterList);

            if (reader != null && reader.HasRows)
            {
                reader.Read();
                {
                    return "" + reader["File_location"] + "|" + reader["Artist"] + "|" + reader["Song Name"] + "|" + reader["Duration"] + "|" + reader["Song_ID"] + "|" + reader["Song_Position"];
                }
            }
            else
            {
                return "";
            }
        }
        
        [WebMethod(true)]
        public string getRoomStatus(int roomID)
        {            
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@Room_ID", roomID));

            SqlDataReader reader;
            reader = SQLUtilities.RunStoredProcedure("GetRoomStatus", parameterList);

            if (reader != null && reader.HasRows)
            {
                reader.Read();
                {
                    return "" + reader["status"];
                }
            }
            else
            {
                return "";
            }            
        }

        [WebMethod(true)]
        public string setRoomStatus(int roomID, string songStatus)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@room_id", roomID));
            parameterList.Add(new SqlParameter("@Status", songStatus));

            
            SQLUtilities.RunStoredProcedure("UpdateRoomStatus", parameterList);

            return songStatus;
        }

        [WebMethod(true)]
        public string checkMasterUser(int roomID)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@Room_ID", roomID));

            SqlDataReader reader;
            reader = SQLUtilities.RunStoredProcedure("GetRoomMaster", parameterList);

            if (reader != null && reader.HasRows)
            {
                reader.Read();
                {
                    return "" + reader["MasterUser"];
                }
            }
            else
            {
                return "0";
            }
        }

        [WebMethod(true)]
        public void updateRoomSong(int userID, int songID, int secondsIntoSong, int roomID)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@user_id", userID));
            parameterList.Add(new SqlParameter("@Song_id", songID));
            parameterList.Add(new SqlParameter("@Seconds_into_song", secondsIntoSong));
            parameterList.Add(new SqlParameter("@room_id", roomID));

            SQLUtilities.RunStoredProcedure("Update_Room_Song", parameterList);
        }

        [WebMethod(true)]
        public void updateSongHistoryTable(int roomID, int songID)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@Room_ID", roomID));
            parameterList.Add(new SqlParameter("@song_ID", songID));

            SQLUtilities.RunStoredProcedure("UpdateSongHistoryTable", parameterList);
        }

        [WebMethod(true)]
        public void updateHeartBeats(int roomID, int userID)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@room_id", roomID));
            parameterList.Add(new SqlParameter("@User_id", userID));

            SQLUtilities.RunStoredProcedure("UpdateHeartBeats", parameterList);
        }

        [WebMethod(true)]
        public int updateSongRating(int userID, int songID, int rating)
        {            
            List<SqlParameter> parameterList = new List<SqlParameter>();           
            parameterList.Add(new SqlParameter("@user_ID", userID));
            parameterList.Add(new SqlParameter("@Song_ID", songID));
            parameterList.Add(new SqlParameter("@Rating", rating));

            SqlParameter outputParameter = new SqlParameter("@Complete", SqlDbType.Int);

            int success = SQLUtilities.RunStoredProcedure("Update_User_Song_Rating", parameterList, outputParameter);

            return success;            
        }


        [WebMethod(true)]
        public string getUsersInRoom(int roomID)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@room_id", roomID));

            SqlDataReader reader = SQLUtilities.RunStoredProcedure("GetUsersInRoom", parameterList);

            if (reader != null && reader.HasRows)
            {
                string users = "";
                for (int i = 0; i < 5; i++)
                {
                    if (reader.Read())
                    {
                        if(i == 0)
                            users = reader[0].ToString();
                        else
                            users = users + "|" + reader[0];
                    }
                    else
                        users = users + "|Open Spot";
                }
                return users;
            }
            else
                return "Error: No users are in the room!";
        }


        [WebMethod(true)]
        public string getRoomSongHistory(int roomID)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@room_id", roomID));

            SqlDataReader reader = SQLUtilities.RunStoredProcedure("GetRoomSongHistory", parameterList);

            if (reader != null && reader.HasRows)
            {
                string songs = "";
                for (int i = 0; i < 10; i++)
                {
                    if (reader.Read())
                    {
                        if (i == 0)
                            songs = reader[1] + " - " + reader[0];
                        else
                            songs = songs + "|" + reader[1] + " - " + reader[0];
                    }
                    else
                        songs = songs + "|No Song";
                }
                return songs;
            }
            else
                return "This is the first song: no history available.";
        }

        
        [WebMethod(true)]
        public string getAvgSongRating(int songID, int roomID)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();           
            parameterList.Add(new SqlParameter("@Room_ID", roomID));
            parameterList.Add(new SqlParameter("@Song_ID", songID));
            SqlDataReader reader = SQLUtilities.RunStoredProcedure("Get_Average_Song_rating", parameterList);

            if (reader != null && reader.HasRows)
            {
                reader.Read();
                return reader[0].ToString();
            }
            else
                return "Song has not been previously rated!";
        }

        [WebMethod(true)]
        public string getUpdatedSongRating(int roomID)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@Room_ID", roomID));
            SqlDataReader reader = SQLUtilities.RunStoredProcedure("Get_Updated_Song_Rating", parameterList);

            if (reader != null && reader.HasRows)
            {
                reader.Read();
                return reader[0].ToString() + " has rated " + reader[1].ToString() + " " + reader[2].ToString() + ".  Woot!";
            }
            else
                return "";
        }

        [WebMethod(true)]
        public string getChatComment(int roomID)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@Room_id", roomID));
            SqlDataReader reader = SQLUtilities.RunStoredProcedure("GetChats", parameterList);

            if (reader != null && reader.HasRows)
            {
                string chat = "";
                while(reader.Read())
                {
                    if (chat.Equals(""))
                        chat = reader[0] + " says: " + reader[1] + "\n";
                    else
                        chat = chat + reader[0] + " says: " + reader[1] + "\n";
                }
                return chat;
            }
            else
                return "";
        }

        
        [WebMethod(true)]
        public void insertChatComment(int userID, int roomID, string chatComment)
        {            
            List<SqlParameter> parameterList = new List<SqlParameter>();           
            parameterList.Add(new SqlParameter("@user_id", userID));
            parameterList.Add(new SqlParameter("@Room_id", roomID));
            parameterList.Add(new SqlParameter("@Chat", chatComment));

            SQLUtilities.RunStoredProcedure("InsertChat", parameterList);                  
        }


        [WebMethod(true)]
        public string getSongComment(int songID, int time)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@song_id", songID));
            parameterList.Add(new SqlParameter("@Time", time));

            SqlDataReader reader = SQLUtilities.RunStoredProcedure("GetNewComment", parameterList);

            if (reader != null && reader.HasRows)
            {
                reader.Read();
                return "At " + reader[2].ToString() + " seconds, " + reader[0].ToString() + " says: " + reader[1].ToString() + "  Woot!";
            }
            else
                return "";
        }


        [WebMethod(true)]
        public void insertSongComment(int songID, int userID, int time, string songComment)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@song_id", songID));
            parameterList.Add(new SqlParameter("@user_id", userID));
            parameterList.Add(new SqlParameter("@Time", time));
            parameterList.Add(new SqlParameter("@Comment", songComment));

            SQLUtilities.RunStoredProcedure("InsertNewComment", parameterList);
        }

    }
}
